package com.qing.expert.modules.admin.controller;

import com.qing.expert.common.response.R;
import com.qing.expert.modules.admin.service.AdminExpertService;
import com.qing.expert.modules.admin.vo.request.AuditRequest;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.qing.expert.common.annotation.OperationLog;
import com.qing.expert.common.exception.BusinessException;
import com.qing.expert.modules.expert.entity.ExpertApply;

@Slf4j
@RestController
@RequestMapping("/api/admin/experts")
public class AdminExpertController {

    @Resource
    private AdminExpertService adminExpertService;

    /**
     * 查询达人申请
     */
    @GetMapping("/apply/{applyId}")
    public R<ExpertApply> getExpertApply(@PathVariable String applyId) {
        try {
            ExpertApply apply = adminExpertService.getExpertApply(applyId);
            return R.ok(apply);
        } catch (BusinessException e) {
            log.warn("查询达人申请失败: {}", e.getMessage());
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error("查询达人申请发生系统错误", e);
            return R.error("查询失败，请稍后重试");
        }
    }

    /**
     * 审核达人申请
     */
    @OperationLog(action = "audit_expert_apply", detail = "审核达人申请：#{#applyId}")
    @PutMapping("/apply/{applyId}")
    public R<Void> auditExpertApply(
            @PathVariable String applyId,
            @Validated @RequestBody AuditRequest request) {
        try {
            adminExpertService.auditExpertApply(applyId, request.getAction());
            return R.ok();
        } catch (BusinessException e) {
            log.warn("审核达人申请失败: {}", e.getMessage());
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error("审核达人申请发生系统错误", e);
            return R.error("审核失败，请稍后重试");
        }
    }
}